<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="type">
        <div class="treeselect" style="width: 300px">
          <treeselect v-model="queryParams.kcType" :options="typeOptions"
                      :show-count="true"      :normalizer="normalizer" @select="handleNodeClickquery" placeholder="选择类型"/>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:kc:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:kc:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:kc:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:kc:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="课程名称" align="center" prop="title" />
<!--      <el-table-column label="课程分类" align="center" prop="kcType" />-->
      <el-table-column label="课程分类" align="center" prop="kcTypeName" />

      <el-table-column label="课程封面" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="100">
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptName" width="100">
      </el-table-column>
      <el-table-column label="岗位" align="center" prop="postName" width="100">
      </el-table-column>
      <el-table-column label="工龄范围(月)" align="center" prop="glMin" width="100">
        <template slot-scope="scope">
          {{ scope.row.glMin }} - {{ scope.row.glMax }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"></el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:kc:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:kc:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="课程名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程分类" prop="kcType">
          <div class="treeselect">
            <treeselect v-model="form.kcType" :options="typeOptions"
                        :show-count="true"      :normalizer="normalizer" @select="handleNodeClick" placeholder="选择类型"/>
          </div>
        </el-form-item>
        <el-form-item label="课程封面" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="视频" prop="video">
          <FileUploadNew v-model="form.video"  v-if="open"   :businessId="form.id" :limit="1" :fileSize="200"  :file-type="['mp4']" />
        </el-form-item>

        <el-form-item label="部门" prop="deptList">
          <el-select  v-model="form.deptList" placeholder="请选择部门"    clearable   multiple  >
            <el-option
              v-for="item in deptOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            />
          </el-select>
        </el-form-item>



        <el-form-item label="岗位" prop="postList">
          <el-select v-model="form.postList" placeholder="请选择岗位"    clearable   multiple  >
            <el-option
              v-for="item in postOptions"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="工龄最小值(月)" prop="glMin">
          <el-input-number v-model="form.glMin" placeholder="请输入工龄最小值"  :min="0" :max="1000"/>
        </el-form-item>
        <el-form-item label="工龄最大值(月)" prop="glMax">
          <el-input-number v-model="form.glMax" placeholder="请输入工龄最大值"   :min="1"  :max="1000"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKc, getKc, delKc, addKc, updateKc } from "@/api/business/kc";
import { listType, getType, delType, addType, updateType } from "@/api/business/kctype";
import { listPost, getPost, delPost, addPost, updatePost,allPost } from "@/api/system/post";
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { guid } from '@/utils/utils'
import { getDeptlist } from '@/api/system/dept'

export default {
  components: {Treeselect},
  name: "Kc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      kcList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        kcType: null,
        image: null,
        video: null,
        postId: null,
        deptId: null,
        postList:[],
        deptList:[]

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '课程名称不能为空', trigger: 'blur' }
        ],
        kcType: [
          { required: true, message: '课程分类不能为空', trigger: 'blur' }
        ],
        image: [
          { required: true, message: '课程封面不能为空', trigger: 'blur' }
        ],
        postList: [
          { required: true, message: '岗位不能为空', trigger: 'blur' }
        ],
        deptList: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ]

      },
      typeOptions:[],
      postOptions:[],
      deptOptions:[],
    };
  },
  created() {
    this.getList();
    listType().then(response => {
      this.typeOptions = this.handleTree(response.data, "id");
    });
    this.getPostAll()
    this.getDeptAll()
  },
  methods: {
    handleNodeClickquery(data){

    },
   // 获取所有的岗位
    getPostAll(){
      allPost({}).then(response => {
        this.postOptions = response.data;
      });

    },
    getDeptAll(){
      getDeptlist({}).then(response => {
        this.deptOptions = response.data;
      });

    },
    handleNodeClick(data){
      if(data.parentId=='0'){
        this.$modal.msgError("请选择二级节点");
      }
      return;
    },
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listKc(this.queryParams).then(response => {
        this.kcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },

      // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        kcType: null,
        image: null,
        video: null,
        postId: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        deptList:[],
        postList:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程信息";
      // 生成一个uuId
      this.form.id = guid();
      console.log(this.form.id,"kkkkkkkkkkkk")
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKc(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
             // 判断工龄
          if (this.form.glMax == this.form.glMin  ||   this.form.glMax < this.form.glMin) {
            this.$modal.msgError('工龄最大值应该大于工龄最小值')
            return
          }
            updateKc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程信息编号为"' + ids + '"的数据项？').then(function() {
        return delKc(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/kc/export', {
        ...this.queryParams
      }, `kc_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
